#include <iostream>
#include <vector>

int main(){
    int num;
    std::cin>>num;
    int target=0;
    std::cin>>target;
    int target0=target;
    std::vector<int> a(num+1,1);
    int count=num;
    int i;
    while (count!=1){
        
        
        a[target]=0;
        count--;
        for (int i=0; i<target0; i++){
            target+=1;
            if (target>num)target=1;
            while (a[target]==0){
                target++;
                if (target>num)target=1;
            }
        }
        

    }
    for (int i=1;i<num+1;i++){
        if (a[i]==1){std::cout<<i-1<<std::endl;break;}
    }
    return 0;
}